package com.frame.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.frame.domain.Cargo;
import com.frame.domain.City;
import com.frame.domain.Crop;
import com.frame.service.CargoService;
import com.frame.service.CityService;
import com.frame.service.CropService;
import com.frame.utils.Result;
import com.github.pagehelper.PageInfo;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController

public class CargoController {

    @Autowired
    private CargoService cargoService;

    @Autowired
    private CityService cityService;

    @Autowired
    HttpServletResponse response;

    private static String accessKey="TFUFhcf36PBh8JSC";

    private static String secretKey="c1egDsoSPQ2gcZ6PJSpTehiZGbkDyXjC";


    private static String bucketName="test";

    @RequestMapping("/getbaobiao")
    public Result getbaobiao(){


        List<Map<String,String>> list = cargoService.getlist3();

        return Result.success(list);
    }

    @RequestMapping("deleteById")
    public Result getbyId(Integer id)
    {
        boolean b= cargoService.deleteById(id);
        return b?Result.success():Result.fail();
    }

  @RequestMapping("/exportExecel")
    public   void getexport() throws IOException {
        String fileName = URLEncoder.encode("菜单表", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Cargo> list = cargoService.getlist();
        EasyExcel.write(response.getOutputStream(), Cargo.class).sheet("仓库管理").doWrite(list);
  }


    @RequestMapping("/gettype")
    public Result gettype(){

        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        List<City> nodeList = cityService.getlist();

        treeNodeConfig.setIdKey("value");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setDeep(3);
        treeNodeConfig.setParentIdKey("pid");
        treeNodeConfig.setChildrenKey("children");

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<Object>> treeNodes = TreeUtil.<City, Object>build(nodeList, 1, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCityId());
                    tree.setParentId(treeNode.getPid());
                    tree.setName(treeNode.getCityname());
                });
        return Result.success(treeNodes);

    }

    @RequestMapping("uploadFile")
    public String uploadFile(MultipartFile file) throws Exception {
        MinioClient minioClient = MinioClient
                .builder()
                .endpoint("http://127.0.0.1:9000")
                .credentials(accessKey, secretKey)
                .build();
        String filename = UUID.randomUUID() + file.getOriginalFilename();
        try {
            minioClient.putObject(PutObjectArgs.builder().object(filename)
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .contentType(file.getContentType()).bucket(bucketName)
                    .build());
        } catch (Exception e) {
            throw new Exception(e);
        }
        return "http://127.0.0.1:9000/" + bucketName + "/" + filename;
    }

    @RequestMapping("/cargo")
    public Result getCargo(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "3") Integer pageSize){
        PageInfo<Cargo> list= cargoService.getlist1(pageNum,pageSize);
        return Result.success(list);
    }

    @RequestMapping("/getadd")
    public Result getadd(@RequestBody Cargo cargo)
    {
        boolean b= cargoService.addCargo(cargo);

        return b?Result.success():Result.fail();
    }

    @RequestMapping("/getqs")
    public Result getqs(Integer id)
    {
        List<Cargo> getlist=cargoService.getqslist(id);
        return Result.success(getlist);
    }

    @RequestMapping("/getupdate")
    public Result getupdate(@RequestBody Cargo cargo)
    {
        boolean b= cargoService.updateCargo(cargo);

        return b?Result.success():Result.fail();
    }





}
